package com.sun.tetris.ui;

import com.sun.tetris.object.Piece;
import com.sun.tetris.object.Square;
import com.sun.tetris.util.ImageUtil;

import javax.swing.*;
import java.awt.*;

/**
 * @author 烛龙神
 * @Description TODO
 * @date 2021-07-27 22:17
 */
public class GamePanel extends JPanel {

    MainFrame mainFrame;

    private Image background = ImageUtil.getImage("images/background.jpg");

    public GamePanel(MainFrame mainFrame)
    {
        this.mainFrame = mainFrame;
    }

    public void paint(Graphics graphics)
    {
        graphics.drawImage(this.background,0,0,this.getWidth(),this.getHeight(),null);
        Piece currentPiece = this.mainFrame.getCurrentPiece();
        ImageUtil.paintPiece(graphics,currentPiece);
        Square[][] squares = this.mainFrame.getSquares();
        if(squares == null) return;
        for (int i = 0; i < squares.length; i++) {
            for (int j = 0; j < squares[i].length; j++) {
                Square s = squares[i][j];
                if(s != null)
                {
                    graphics.drawImage(s.getImage(),s.getBeginX(),s.getBeginY(),this);
                }
            }
            
        }

    }
}
